package com.ternnetwork.clustersyn.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import com.ternnetwork.clustersyn.model.AppInstance;
import com.ternnetwork.clustersyn.model.Cluster;
import com.ternnetwork.clustersyn.model.ServerNode;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class ClusterConfigUtil {

	public static void main(String[] args) {
		
	
		/*ServerNode node=new ServerNode();
		node.setId(1);
		node.setIp("192.168.0.1");
		node.setName("测试");
		node.setFtpPort(2236);
		AppInstance instance=new AppInstance();
		instance.setBroadcast(true);
		instance.setFtpPassword("1234");
		instance.setFtpUserName("admin");
		instance.setFtpHomeDirectory("D:/work");
		instance.setId(1);
		List<String> synDirList=new ArrayList<String>();
		synDirList.add("/dfdfd");
		instance.setSynDirList(synDirList);
		
		List<AppInstance> appInstanceList=new ArrayList<AppInstance>();
		
		appInstanceList.add(instance);
		node.setAppInstanceList(appInstanceList);
		
		Cluster cluster=new Cluster();
		cluster.setId(1);
		cluster.setCurrentServerNodeId(1);
		List<ServerNode> nodeList=new ArrayList<ServerNode>();
		nodeList.add(node);
		cluster.setServerNodeList(nodeList);
		
		    XStream xstream = new XStream(new DomDriver());
	        xstream.alias("serverNode", ServerNode.class);
	        xstream.alias("appInstance", AppInstance.class);
	        xstream.alias("cluster", Cluster.class);
	       
	        String xml = xstream.toXML(cluster);
	        System.out.println(xml); */

	    }
	
	public static Cluster getCluster(){
		    XStream xstream = new XStream(new DomDriver());
		    xstream.alias("serverNode", ServerNode.class);
	        xstream.alias("appInstance", AppInstance.class);
	        xstream.alias("cluster", Cluster.class);
		    Cluster cte=new Cluster();
		try {
			  File directory = new File("");// 参数为空
			  String projectRoot = directory.getCanonicalPath().replaceAll("\\\\","/").replaceAll("/bin", "");
	          cte = (Cluster) xstream.fromXML(new FileReader(new File(projectRoot+"/conf/cluster.xml")));
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
		return cte;
	}

}
